local math = math
local pairs = pairs
local ipairs = ipairs
local type = type
local table = table
local tabletool = require "tabletool"
local timetool = require "timetool"
local base = require "base"
local filelog = require "filelog"
require "pubenum"
require "sssenum"
require "ssscardvalue"
--[[
    十三水牌型判断
]]
local cardTool = {}

local function calculate_cardtype(card)
    return (card // 100)
end

local function calculate_cardvalue(card)
    return (card % 100)
end

--从大到小排序
local function comps(card1, card2)
    local value1 = calculate_cardvalue(card1)
    local value2 = calculate_cardvalue(card2)
    if value1 > value2 then
        return true
    elseif value1 == value2 and calculate_cardtype(card1) < calculate_cardtype(card2)  then
        return true
    end 
    return false
end

--计算累计牌值
local function calculate_niu_totalvalue(cards)
    local total_card_value = 0
    local card_value
    for _, card in ipairs(cards) do
        card_value = calculate_cardvalue(card)
        if card_value >= 10 then
            total_card_value = total_card_value + 10
        else
            total_card_value = total_card_value + card_value
        end 
    end

    return total_card_value
end

local function cards_shuffle(cards, poker_num, has_joker)
    -- 洗牌
    local index
    local tmp_seed
    tmp_seed = base.RNG()
    if tmp_seed == nil then
        tmp_seed = timetool.get_10ms_time()
    end

    for i = 1, #CardData do
        math.randomseed(tmp_seed + 11*i)    
        index = math.random(#CardData + 1 - i)
        CardData[index], CardData[#CardData + 1 - i] = CardData[#CardData + 1 - i], CardData[index]
    end

    for i = 1, poker_num do
        for index, card in pairs(CardData) do
            if card >= 501 then
                if has_joker == 2 then table.insert(cards, card) end
            else
                table.insert(cards, card)
            end
        end
    end
end
--获得新的牌堆
function cardTool.get_new_cards(poker_num, has_joker)
    if not poker_num or poker_num < 1 then
        poker_num = 1
    end
    if poker_num > 2 then
        poker_num = 2
    end
    local card_data = {}

    cards_shuffle(card_data, poker_num, has_joker)
    return card_data
end

--对传入的牌按牌值由大到小排序
function cardTool.from_ztoa_sort(cards)
    table.sort(cards, comps)
end

function cardTool.has_pairs(cards)
    local num = 0
    local card_value = 0
    for i = 1, #cards do
        num = 1
        card_value = calculate_cardvalue(cards[i])
        for j = i+1, #cards do
            if card_value == calculate_cardvalue(cards[j]) then
                num = num + 1
            end 
        end
        if num >= 2 then
            return true
        end
    end
    return false
end
---从牌中获取一个对子
function cardTool.get_pairs_incards(cards, is_has_joker)
    if not cards or #cards < 2 then return false, {} end
    if is_has_joker == 2 then
        local jokers = {}
        local get_card = {}
        for index, card in ipairs(cards) do
            if card >= 501 then table.insert(jokers, card) end
        end
        local num = 0
        local card_value = 0
        for i = 1, #cards do
            if cards[i] and cards[i] < 501 then 
                num = 1
                card_value = calculate_cardvalue(cards[i])
                get_card = {}
                table.insert(get_card, cards[i])
                for j = i+1, #cards do
                    if cards[j] and cards[j] < 501 then
                        if card_value == calculate_cardvalue(cards[j]) then
                            num = num + 1
                            table.insert(get_card, cards[j])
                        end
                    end
                end
                if num == 2 then
                    if #get_card ~= 2 then
                        return false, {}
                    end
                    return true, get_card
                end
            end
        end
        if #jokers > 0 then
            for index, card in ipairs(jokers) do
                if #get_card < 2 then
                    table.insert(get_card, card)
                end
            end
            if #get_card ~= 2 then return false, {} end
            return true, get_card
        end
        return false, get_card
    else
        local num = 0
        local card_value = 0
        local get_card = {}
        for i = 1, #cards do
            num = 1
            if cards[i] then 
                card_value = calculate_cardvalue(cards[i]) 
            end
            get_card = {}
            for j = i + 1, #cards do
                if cards[j] then
                    if card_value == calculate_cardvalue(cards[j]) then
                        num = num + 1
                        table.insert(get_card, cards[j])
                    end
                end
            end
            if num == 2 then
                if #get_card ~= 2 then return false, {} end
                return true, get_card 
            end
        end
        return false, {}
    end
end
---从牌堆里选三条出来
function cardTool.get_threecard_incards(cards, is_has_joker)
    if not cards or #cards < 3 then return false, {} end
    if is_has_joker == 2 then
        local jokers = {}
        for index, card in ipairs(cards) do
            if card >= 501 then table.insert(jokers, card) end
        end
        local num = 0
        local card_value = 0
        local three_value = 0
        for i = 1, #cards do
            num = 1
            if cards[i] and cards[i] < 501 then card_value = calculate_cardvalue(cards[i]) end
            for j = i+1, #cards do
                if cards[j] and cards[j] < 501 then
                    if card_value == calculate_cardvalue(cards[j]) then
                        num = num + 1
                    end
                end
            end
            if num == 3 then
                three_value = card_value 
                local get_card = {}
                for index, card in ipairs(cards) do
                    if calculate_cardvalue(card) == three_value then
                        table.insert(get_card, card)
                    end
                end
                return true, get_card
            end
        end
        card_value = 0
        for i = 1, #cards do
            num = 1
            if cards[i] and cards[i] < 501 then card_value = calculate_cardvalue(cards[i]) end
            for j = i + 1, #cards do
                if cards[j] and cards[j] < 501 then
                    if card_value == calculate_cardvalue(cards[j]) then
                        num = num + 1
                    end
                end
            end
            if num + #jokers == 3 then
                three_value = card_value
                local get_card = {}
                for index, card in ipairs(cards) do
                    if calculate_cardvalue(card) == three_value and #get_card < 3 then
                        table.insert(get_card, card)
                    end
                end
                for index, joker in ipairs(jokers) do
                    if #get_card < 3 then
                        table.insert(get_card, joker)
                    end
                end
                if #get_card == 3 then return true, get_card end
            end
        end
        return false, {}
    else
        local num = 0
        local card_value = 0
        local three_value = 0
        for i = 1, #cards do
            num = 1
            if cards[i] then card_value = calculate_cardvalue(cards[i]) end
            for j = i + 1, #cards do
                if cards[j] then
                    if card_value == calculate_cardvalue(cards[j]) then
                        num = num + 1
                    end
                end
            end
            if num == 3 then
                three_value = card_value
                local get_card = {}
                for index, card in ipairs(cards) do
                    if three_value == calculate_cardvalue(card) then
                        table.insert(get_card, card)
                    end
                end
                return true, get_card
            end
        end
        return false, {}
    end
end

function cardTool.is_bomb(cards)
    local num = 0
    local card_value = 0
    for i=1, 2 do
        num = 1
        if cards[i] then card_value = calculate_cardvalue(cards[i]) end
        for j=i+1, #cards do
            if cards[j] then
                if card_value == calculate_cardvalue(cards[j]) then
                    num = num + 1
                end
            end
        end
        if num >= 4 then
            return true
        end
    end
    return false
end
----从牌中获取炸弹
function cardTool.get_bomb_incards(cards, is_has_joker)
    if not cards or #cards < 4 then return false, {} end
    if is_has_joker == 2 then
        local jokers = {}
        for index, card in ipairs(cards) do
            if card >= 501 then table.insert(jokers, card) end
        end
        local num = 0
        local card_value = 0
        local bomb_value = 0
        for i = 1, #cards do
            num = 1
            if cards[i] and cards[i] < 501 then card_value = calculate_cardvalue(cards[i]) end
            for j = i+1, #cards do
                if cards[j] and cards[j] < 501 then
                    if card_value == calculate_cardvalue(cards[j]) then
                        num = num + 1
                    end
                end
            end
            if num == 4 then
                bomb_value = card_value
                local get_card = {}
                for index, card in ipairs(cards) do
                    if bomb_value == calculate_cardvalue(card) then
                        table.insert(get_card, card)
                    end
                end
                return true, get_card
            elseif num + #jokers >= 4 then
                bomb_value = card_value
                local get_card = {}
                for index, card in ipairs(cards) do
                    if bomb_value == calculate_cardvalue(card) then
                        table.insert(get_card, card)
                    end
                end
                for index, card in ipairs(jokers) do
                    if #get_card < 4 then
                        table.insert(get_card, card)
                    end
                end
                return true, get_card
            end
        end
        return false, {}
    else
        local num = 0
        local card_value = 0
        local bomb_value = 0
        for i = 1, #cards do
            num = 1
            if cards[i] then card_value = calculate_cardvalue(cards[i]) end
            for j = i + 1, #cards do
                if cards[j] then
                    if card_value == calculate_cardvalue(cards[j]) then
                        num = num + 1
                    end
                end
            end
            if num == 4 then
                bomb_value = card_value
                local get_card = {}
                for index, card in ipairs(cards) do
                    if bomb_value == calculate_cardvalue(card) then
                        table.insert(get_card, card)
                    end
                end
                return true, get_card
            end
        end
        return false, {}
    end
end

function cardTool.calculate_cardvalue(card)
    return calculate_cardvalue(card)
end

function cardTool.card_to_string()
    
end
---判断是否是五同
function cardTool.is_wutong(cards, is_has_joker)
    if not cards or not is_has_joker then return false end
    if #cards ~= 5 then return false end
    if is_has_joker == 2 then
        local jokers = {}
        for index, card in ipairs(cards) do
            if card >= 501 then table.insert(jokers, card) end
        end
        local num = 0
        local card_value = 0
        for i = 1, #cards do
            num = 1
            if cards[i] and cards[i] < 501 then card_value = calculate_cardvalue(cards[i]) end
            for j = i+1, #cards do
                if cards[j] and cards[j] < 501 then
                    if card_value == calculate_cardvalue(cards[j]) then
                        num = num + 1
                    end
                end
            end
            if num + #jokers == 5 then
                return true
            end
        end
    else
        local num = 0
        local card_value = 0
        for i = 1, #cards do
            num = 1
            if cards[i] then card_value = calculate_cardvalue(cards[i]) end
            for j = i+1, #cards do
                if cards[j] then
                    if card_value == calculate_cardvalue(cards[j]) then
                        num = num + 1
                    end
                end
            end
            if num == 5 then
                return true
            end
        end
    end
    return false
end
---获取五同
function cardTool.get_wt_incards(cards, is_has_joker)
    local is_have = false
    local get_card = {}
    if not cards or #cards < 5 then return is_have, get_card end
    if is_has_joker == 2 then
        local jokers = {}
        local temp_cards = tabletool.deepcopy(cards)
        for index, card in pairs(temp_cards) do
            if card >= 501 then table.insert(jokers, card) end
        end
        local wt_cardvalue = 0
        local num = 0
        local card_value = 0
        for i = 1, #temp_cards do
            if temp_cards[i] < 501 then
                num = 1
                if temp_cards[i] then card_value = calculate_cardvalue(temp_cards[i]) end
                for j = i+1, #temp_cards do
                    if temp_cards[j] and temp_cards[j] < 501 then
                        if card_value == calculate_cardvalue(temp_cards[j]) then num = num + 1 end
                    end
                end
                if num + #jokers >= 5 then
                    wt_cardvalue = card_value
                    break
                end
            end
        end
        if wt_cardvalue ~= 0 then
            is_have = true
            for index, temp_card in pairs(temp_cards) do
                if wt_cardvalue == calculate_cardvalue(temp_card) and #get_card < 5 then
                    table.insert(get_card, temp_card)
                end
            end
            for index, joker in ipairs(jokers) do
                if #get_card < 5 then table.insert(get_card, joker) end
            end
            if #get_card ~= 5 then
                is_have = false
                get_card = {}
            end
            return is_have, get_card
        else
            return is_have, get_card
        end
    else
        local temp_cards = tabletool.deepcopy(cards)
        local wt_cardvalue = 0
        local num = 0
        local card_value = 0
        for i = 1, #temp_cards do
            num = 1
            if temp_cards[i] then card_value = calculate_cardvalue(temp_cards[i]) end
            for j = i+1, #temp_cards do
                if temp_cards[j] then
                    if card_value == calculate_cardvalue(temp_cards[j]) then
                        num = num + 1
                    end
                end
            end
            if num == 5 then
                wt_cardvalue = card_value
                break
            end
        end
        if wt_cardvalue ~= 0 then
            is_have = true
            for index, temp_card in ipairs(temp_cards) do
                if wt_cardvalue == calculate_cardvalue(temp_card) and #get_card < 5 then
                    table.insert(get_card, temp_card)
                end
            end
            if #get_card ~= 5 then
                is_have = false
                get_card = {}
            end
            return is_have, get_card
        else
            return is_have, get_card
        end
    end
end

--判断是否同花顺
function cardTool.is_straightflush(cards, is_has_joker)
    local is_tonghua = cardTool.is_withflowers(cards, is_has_joker)
    local is_shunzi = cardTool.is_flush(cards, is_has_joker)
    ---filelog.sys_error("---------is_straightflush-------", is_tonghua, is_shunzi)
    return (is_tonghua and is_shunzi)
end

--从牌中提取同花顺
function cardTool.get_ths_incards(cards, is_has_joker)
    local is_have, get_card = false, {}
    if not cards or #cards < 3 then return is_have, get_card end
    local temp_cards = tabletool.deepcopy(cards)
    for i = 1, 3 do
        local is_shunzi, shunzi_get_cards = cardTool.get_sz_incards(temp_cards, is_has_joker)
        if is_shunzi == true then
            local is_th = cardTool.is_withflowers(shunzi_get_cards, is_has_joker)
            if is_th == true then
                for index, card in ipairs(shunzi_get_cards) do
                    table.insert(get_card, card)
                end
                is_have = true
                return is_have, get_card
            end
        end
    end
    return is_have, get_card
end

--判断是否是铁支
function cardTool.is_tiezhi(cards, is_has_joker)
    if not cards or not is_has_joker then return false end
    if #cards ~= 5 then return false end
    if is_has_joker == 2 then
        local jokers = {}
        for index, card in ipairs(cards) do
            if card >= 501 then table.insert(jokers, card) end
        end
        local num = 0
        local card_value = 0
        for i=1, 2 do
            num = 1
            if cards[i] and cards[i] < 501 then card_value = calculate_cardvalue(cards[i]) end
            for j=i+1, #cards do
                if cards[j] and cards[j] < 501 then
                    if card_value == calculate_cardvalue(cards[j]) then
                        num = num + 1
                    end
                end
            end
            if num + #jokers == 4 then
                return true
            end
        end
    else
        local num = 0
        local card_value = 0
        for i=1, 2 do
            num = 1
            if cards[i] then card_value = calculate_cardvalue(cards[i]) end
            for j=i+1, #cards do
                if cards[j] then
                    if card_value == calculate_cardvalue(cards[j]) then
                        num = num + 1
                    end
                end
            end
            if num == 4 then
                return true
            end
        end
    end
    return false
end
---获取铁支
function cardTool.get_tz_incards(cards, is_has_joker)
    local is_have, get_card = false, {}
    if not cards or #cards < 5 then return is_have, get_card end
    local temp_cards = tabletool.deepcopy(cards)
    local is_has_bomb, get_bomb_cards = cardTool.get_bomb_incards(temp_cards, is_has_joker)
    if is_has_bomb == true then
        local temp_tempcards = tabletool.deepcopy(temp_cards)
        temp_cards = {}
        for index, temp_card in ipairs(temp_tempcards) do
            local equal = false
            for index, card in ipairs(get_bomb_cards) do
                if card == temp_card then
                    equal = true
                    break
                end
            end
            if equal == false then table.insert(temp_cards, temp_card) end
        end
        for index, card in ipairs(get_bomb_cards) do
            table.insert(get_card, card)
        end
        for i = #temp_cards, 1, -1 do
            if #get_card < 5 then
                table.insert(get_card, temp_cards[i])
            end
        end
        if #get_card == 5 then
            is_have = true
            return is_have, get_card
        end
    end
    return is_have, get_card
end

--@des 判断是否葫芦
function cardTool.is_bottlegourd(cards, is_has_joker)
    if not cards or not is_has_joker then return false end
    if #cards ~= 5 then return false end
    if is_has_joker == 2 then
        local jokers = {}
        for index, card in ipairs(cards) do
            if card >= 501 then table.insert(jokers, card) end
        end
        local num = 0
        local card_value = 0
        for i = 1, 3 do
            if cards[i] < 501 then
                num = 1
                card_value = calculate_cardvalue(cards[i])
                for j = i+1, #cards do
                    if cards[j] < 501 then
                        if card_value == calculate_cardvalue(cards[j]) then
                            num = num + 1
                        end
                    end
                end
                if num == 3 then
                    local duizi_card_value = 0
                    local duizi_num = 0
                    for k=1, #cards do
                        if cards[k] < 501 then
                            if calculate_cardvalue(cards[k]) ~= card_value then
                                if duizi_card_value == 0 then
                                    duizi_card_value = calculate_cardvalue(cards[k])
                                    duizi_num = duizi_num + 1
                                elseif duizi_card_value == calculate_cardvalue(cards[k]) then
                                    duizi_num = duizi_num + 1
                                end
                            end
                        end
                    end
                    if duizi_num == 0 then
                        if #jokers == 2 then return true end
                    elseif duizi_num == 1 then
                        if #jokers == 1 then return true end
                    elseif duizi_num == 2 then
                        return true
                    end
                elseif num < 3 then
                    if num + #jokers >= 3 then
                        for i = 1, 3-num do
                            table.remove(jokers, 1)
                        end
                        local duizi_card_value = 0
                        local duizi_num = 0
                        for k = 1, #cards do
                            if cards[k] < 501 then
                                if calculate_cardvalue(cards[k]) ~= card_value then
                                    if duizi_card_value == 0 then
                                        duizi_card_value = calculate_cardvalue(cards[k])
                                        duizi_num = duizi_num + 1
                                    elseif duizi_card_value == calculate_cardvalue(cards[k]) then
                                        duizi_num = duizi_num + 1
                                    end
                                end
                            end
                        end
                        if duizi_num == 0 then
                            if #jokers == 2 then return true end
                        elseif duizi_num == 1 then
                            if #jokers == 1 then return true end
                        elseif duizi_num == 2 then
                            return true
                        end
                    end
                end
            end
        end
        return false
    else
        local num = 0
        local card_value = 0
        for i=1, 3 do
            num = 1
            card_value = calculate_cardvalue(cards[i])
            for j=i+1, #cards do
                if card_value == calculate_cardvalue(cards[j]) then
                    num = num + 1
                end
            end
            if num >= 3 then
                local duizi_card_value = 0
                for k=1, #cards do
                    if calculate_cardvalue(cards[k]) ~= card_value then
                        if duizi_card_value == 0 then
                            duizi_card_value = calculate_cardvalue(cards[k])
                        elseif duizi_card_value == calculate_cardvalue(cards[k]) then
                            return true
                        end
                    end
                end
            end
        end        
    end
    return false
end
---获取葫芦
function cardTool.get_hl_incards(cards, is_has_joker)
    local is_have, get_card = false, {}
    if not cards or #cards < 5 then return is_have, get_card end
    ----先获取三条
    local temp_cards = tabletool.deepcopy(cards)

    local is_has_st, get_st_cards = cardTool.get_threecard_incards(temp_cards, is_has_joker)
    if is_has_st ~= true then
        return is_have, get_card
    end
    local temp_tempcards = tabletool.deepcopy(temp_cards)
    temp_cards = {}
    for index, temp_card in ipairs(temp_tempcards) do
        local equal = false
        for index, card in ipairs(get_st_cards) do
            if card == temp_card then
                equal = true
                break
            end
        end
        if equal == false then table.insert(temp_cards, temp_card) end
    end
        
    for index, card in ipairs(get_st_cards) do
        table.insert(get_card, card)
    end

    local is_has_pairs, get_pair_cards = cardTool.get_pairs_incards(temp_cards, is_has_joker)
    ---filelog.sys_error("-------is_has_pairs, get_pair_cards------", is_has_pairs, get_pair_cards)
    if is_has_pairs == false then
        return is_have, {}
    end
    temp_tempcards = {}
    temp_tempcards = tabletool.deepcopy(temp_cards)
    temp_cards = {}
    for index, temp_card in ipairs(temp_tempcards) do
        local equal = false
        for index, card in ipairs(get_pair_cards) do
            if card == temp_card then
                equal = true
                break
            end
        end
        if equal == false then table.insert(temp_cards, temp_card) end
    end
    for index, card in ipairs(get_pair_cards) do
        if #get_card < 5 then
            table.insert(get_card, card)
        end
    end
    if #get_card == 5 then
        is_have = true
        return is_have, get_card
    else
        return false, {}
    end
end

--判断是否同花
function cardTool.is_withflowers(cards, is_has_joker)
    if is_has_joker == 2 then
        local card_type = calculate_cardtype(cards[1])
        for i = 2,#cards do
            if card_type ~= calculate_cardtype(cards[i]) and cards[i] < 501 then
                return false
            end
        end
        return true
    else
        local card_type = calculate_cardtype(cards[1])
        for i=2, #cards do
            if card_type ~= calculate_cardtype(cards[i]) then
                return false
            end 
        end
        return true
    end    
end

---获取同花
function cardTool.get_th_incards(cards, is_has_joker)
    local is_have, get_card = false, {}
    if not cards or #cards < 3 then return is_have, get_card end
    local cards_num = #cards
    if is_has_joker == 2 then
        local jokers = {}
        for index, card in ipairs(cards) do
            if card >= 501 then table.insert(jokers, card) end
        end
        local card_value = 0
        local card_color_value = 0
        local num = 0
        for i = 1, #cards do
            if cards[i] < 501 and cards[i] then
                num = 1
                get_card = {}
                table.insert(get_card, cards[i])
                card_color_value = calculate_cardtype(cards[i])
                for j = i + 1, #cards do
                    if cards[j] and cards[j] < 501 then
                        if card_color_value == calculate_cardtype(cards[j]) then
                            num = num + 1
                            if cards_num == 3 then
                                if #get_card < 3 then table.insert(get_card, cards[j]) end
                            else
                                if #get_card < 5 then table.insert(get_card, cards[j]) end
                            end
                        end
                    end
                end
                if num == 5 then
                    is_have = true
                    return is_have, get_card
                elseif #jokers + num >= 5 then
                    is_have = true
                    for index, joker in ipairs(jokers) do
                        if #get_card < 5 then
                            table.insert(get_card, joker)
                        end
                    end
                    return is_have, get_card
                else
                    if cards_num == 3 then
                        if num == 3 then
                            is_have = true
                        elseif #jokers + num == 3 then 
                            is_have = true
                            for index, joker in ipairs(jokers) do
                                if #get_card < 3 then
                                    table.insert(get_card, joker)
                                end
                            end
                        end
                        if #get_card == 3 then return is_have, get_card end
                    end
                end
            end
        end
    else
        local card_color_value = 0
        local num = 0
        for i = 1, #cards do
            num = 1
            get_card = {}
            table.insert(get_card, cards[i])
            card_color_value = calculate_cardtype(cards[i])
            for j = i + 1, #cards do
                if card_color_value == calculate_cardtype(cards[j]) then
                    num = num + 1
                    table.insert(get_card, cards[j])
                end
            end
            if num == 5 then
                is_have = true
                return is_have, get_card
            elseif num == 3 and num == cards_num then
                is_have = true
                return is_have, get_card
            end
        end
    end
    return is_have, get_card
end

--判断是否顺子
function cardTool.is_flush(cards, is_has_joker)
    if not cards or #cards < 5 then return false end
    if is_has_joker == 2 then
        local jokers = {}
        for index, card in ipairs(cards) do
            if card >= 501 then table.insert(jokers, {card = card, as_value = 0}) end
        end
        local temp_cards = {}
        for index, card in ipairs(cards) do
            if card < 501 then table.insert(temp_cards, card) end
        end
        --filelog.sys_error("---------temp_cards--------", temp_cards)
        if #temp_cards == 0 then
            return false
        elseif #temp_cards == 1 then
            if #jokers == 2 then return true end
            return false
        else
            if calculate_cardvalue(temp_cards[1]) == 14 and calculate_cardvalue(temp_cards[#temp_cards]) <= 5 then
                local card_value = calculate_cardvalue(temp_cards[#temp_cards])
                for i = #temp_cards-1, 2, -1 do
                    if card_value ~= (calculate_cardvalue(temp_cards[i]) - 1) then
                        if #jokers == 0 then
                            return false
                        else
                            local is_des = false
                            local diff_num = (calculate_cardvalue(temp_cards[i]) - 1) - card_value
                            if diff_num > #jokers then return false end
                            for k = 1, diff_num do
                                for index, joker in ipairs(jokers) do
                                    if joker.card >= 501 and joker.as_value == 0 then
                                        is_des = true
                                        joker.as_value = card_value + 1
                                        break
                                    end
                                end
                                if is_des == true then
                                    card_value = card_value + 1
                                    if k == diff_num then
                                        card_value = calculate_cardvalue(temp_cards[i])
                                    end
                                else
                                    return false
                                end
                            end
                        end
                    else
                        card_value = calculate_cardvalue(temp_cards[i])
                    end
                end
            elseif calculate_cardvalue(temp_cards[#temp_cards]) == 2 then
                local card_value = calculate_cardvalue(temp_cards[#temp_cards])
                for i = #temp_cards-1, 1, -1 do
                    if card_value ~= (calculate_cardvalue(temp_cards[i]) - 1) then
                        if #jokers == 0 then
                            return false
                        else
                            local is_des = false
                            local diff_num = (calculate_cardvalue(temp_cards[i]) - 1) - card_value
                            if diff_num > #jokers then return false end
                            for k = 1, diff_num do
                                for index, joker in ipairs(jokers) do
                                    if joker.card >= 501 and joker.as_value == 0 then
                                        is_des = true
                                        joker.as_value = card_value + 1
                                        break
                                    end
                                end
                                if is_des == true then
                                    card_value = card_value + 1
                                    if k == diff_num then
                                        card_value = calculate_cardvalue(temp_cards[i])
                                    end
                                else
                                    return false
                                end
                            end
                        end
                    else
                        card_value = calculate_cardvalue(temp_cards[i])
                    end
                end
            else
                local card_value = calculate_cardvalue(temp_cards[1])
                for i = 2, #temp_cards do
                    if card_value ~= (calculate_cardvalue(temp_cards[i]) + 1) then
                        if #jokers == 0 then
                            return false
                        else
                            local is_des = false
                            local diff_num = card_value - (calculate_cardvalue(temp_cards[i]) + 1)
                            if diff_num > #jokers then return false end
                            for k = 1, diff_num do
                                for index, joker in ipairs(jokers) do
                                    if joker.card >= 501 and joker.as_value == 0 then
                                        is_des = true
                                        joker.as_value = card_value - 1
                                        break
                                    end
                                end
                                if is_des == true then
                                    card_value = card_value - 1
                                    if k == diff_num then
                                        card_value = calculate_cardvalue(temp_cards[i])
                                    end
                                else
                                    return false
                                end
                            end
                        end
                    else
                        card_value = calculate_cardvalue(temp_cards[i])
                    end
                end
            end
        end
    else
        local card_value = calculate_cardvalue(cards[1])
        for i=2, #cards do
            if card_value ~= (calculate_cardvalue(cards[i]) + 1) then
                return false
            else
                card_value = calculate_cardvalue(cards[i])
            end        
        end
    end
    return true
end

local function check_is_have(structs, card_value)
    local is_have = false
    for index, cardobj in ipairs(structs) do
        if card_value == cardobj.card_value then
            is_have = true
            break
        end
    end
    return is_have
end

--从大到小排序
local function compx(card1, card2)
    local value1 = card1.card_value
    local value2 = card2.card_value
    if value1 > value2 then
        return true
    elseif value1 == value2 and calculate_cardtype(card1.card) < calculate_cardtype(card2.card)  then
        return true
    end 
    return false
end

----获取顺子
function cardTool.get_sz_incards(cards, is_has_joker)
    local is_have, get_card = false, {}
    if not cards or #cards < 3 then return is_have, get_card end
    local all_cards_num = #cards
    if is_has_joker == 2 then
        local jokers = {}
        local one_value_cards = {}
        for index, card in ipairs(cards) do
            local card_value = calculate_cardvalue(card)
            if card >= 501 then table.insert(jokers, {card = card, as_value = 0}) end
            local is_have = check_is_have(one_value_cards, card_value)
            if is_have == false and card < 501 then table.insert(one_value_cards, {card = card, card_value = card_value} ) end
        end
        if #one_value_cards + #jokers < 3 or #one_value_cards + #jokers == 4 then
            return false, {}
        end
        ----cardTool.from_ztoa_sort(one_value_cards)
        table.sort(one_value_cards, function(one, two)
            return compx(one, two)
        end)

        --filelog.sys_error("--------one_value_cards------", one_value_cards)
        if one_value_cards[#one_value_cards].card_value == 2 or
            (one_value_cards[1].card_value == 14 and one_value_cards[#one_value_cards].card_value <= 5) then
            if #one_value_cards <= 5 and one_value_cards[#one_value_cards].card_value <= 5 and one_value_cards[1].card_value == 14 then
                one_value_cards[1].card_value = 1
                table.sort(one_value_cards, function(one, two)
                    return compx(one, two)
                end)
            end
            for i = #one_value_cards, 1, -1 do
                local card_value = one_value_cards[i].card_value
                get_card = {}
                table.insert(get_card, one_value_cards[i].card)
                for j = i - 1, 1, -1  do
                    if one_value_cards[j] then
                        if card_value ~= (one_value_cards[j].card_value - 1) then
                            if #jokers == 0 then
                                break
                            else
                                local is_des = false
                                local diff_num = (one_value_cards[j].card_value - 1) - card_value
                                local joker_value = 0
                                if diff_num <= #jokers then
                                    for k = 1, diff_num do
                                        for index, joker in ipairs(jokers) do
                                            if joker.card >= 501 and joker.as_value == 0 then
                                                is_des = true
                                                joker.as_value = card_value + 1
                                                joker_value = joker.card
                                                break
                                            end
                                        end
                                        if is_des == true then
                                            card_value = card_value + 1
                                            if all_cards_num == 3 then
                                                if #get_card < 3 then 
                                                    table.insert(get_card, joker_value)
                                                    if card_value + 1 == one_value_cards[j].card_value then
                                                        if #get_card < 3 then table.insert(get_card, one_value_cards[j].card) end
                                                    end 
                                                end
                                                if #get_card == 3 then return true, get_card end
                                            else
                                                if #get_card < 5 then 
                                                    table.insert(get_card, joker_value)
                                                    -- if card_value + 1 == one_value_cards[j].card_value then
                                                    --     if #get_card < 5 then table.insert(get_card, one_value_cards[j].card) end
                                                    -- end  
                                                end
                                                if #get_card == 5 then return true, get_card end
                                            end
                                            if k == diff_num then
                                                card_value = one_value_cards[j].card_value
                                            end
                                        else
                                            break
                                        end
                                    end
                                else

                                end
                            end
                        else
                            if all_cards_num == 3 then
                                if #get_card < 3 then table.insert(get_card, one_value_cards[j].card) end
                                if #get_card == 3 then return true, get_card end
                            else
                                if #get_card < 5 then table.insert(get_card, one_value_cards[j].card) end
                                if #get_card == 5 then return true, get_card end
                            end
                            card_value = one_value_cards[j].card_value
                        end
                    end
                end
                if all_cards_num == 3 then
                    if #get_card < 3 then
                        if #get_card + #jokers >= 3 then
                            for index, joker in ipairs(jokers) do
                                if #get_card < 3 and joker.as_value == 0 then
                                    table.insert(get_card, joker.card)
                                    joker.as_value = 1
                                end
                            end
                        end
                    end
                    if #get_card == 3 then return true, get_card end
                else
                    if #get_card < 5 then
                        if #get_card + #jokers >= 5 then
                            for index, joker in ipairs(jokers) do
                                if #get_card < 5 and joker.as_value == 0 then
                                    table.insert(get_card, joker.card)
                                    joker.as_value = 1
                                end
                            end
                        end
                    end
                    if #get_card == 5 then return true, get_card end
                end 
            end
        else
            for i = 1, #one_value_cards do
                local card_value = calculate_cardvalue(one_value_cards[i].card)
                get_card = {}
                table.insert(get_card, one_value_cards[i].card)
                for j = i + 1, #one_value_cards do
                    if card_value ~= (calculate_cardvalue(one_value_cards[j].card) + 1) then
                        if #jokers == 0 then
                            break
                        else
                            local is_des = false
                            local diff_num = card_value - (calculate_cardvalue(one_value_cards[j].card) + 1)
                            local joker_value = 0
                            if diff_num > #jokers then
                                break
                            end
                            for k = 1, diff_num do
                                for index, joker in ipairs(jokers) do
                                    if joker.card >= 501 and joker.as_value == 0 then
                                        is_des = true
                                        joker_value = joker.card
                                        joker.as_value = card_value - 1
                                        break
                                    end
                                end
                                if is_des == true then
                                    card_value = card_value - 1
                                    if all_cards_num == 3 then
                                        if #get_card < 3 then table.insert(get_card, joker_value) end
                                        if #get_card == 3 then return true, get_card end
                                    else
                                        if #get_card < 5 then table.insert(get_card, joker_value) end
                                        if #get_card == 5 then return true, get_card end
                                    end
                                    if k == diff_num then
                                        card_value = calculate_cardvalue(one_value_cards[j].card)
                                        if all_cards_num == 3 then
                                            if #get_card < 3 then table.insert(get_card, one_value_cards[j].card) end
                                        else
                                            if #get_card < 5 then table.insert(get_card, one_value_cards[j].card) end
                                        end
                                    end
                                else
                                    break
                                end
                            end
                        end
                    else
                        if all_cards_num == 3 then
                            if #get_card < 3 then table.insert(get_card, one_value_cards[j].card) end
                            if #get_card == 3 then return true, get_card end
                        else
                            if #get_card < 5 then table.insert(get_card, one_value_cards[j].card) end
                            if #get_card == 5 then return true, get_card end
                        end 
                        card_value = calculate_cardvalue(one_value_cards[j].card)
                    end
                end
                if all_cards_num == 3 then
                    if #get_card < 3 then
                        if #get_card + #jokers >= 3 then
                            for index, joker in ipairs(jokers) do
                                if #get_card < 3 and joker.as_value == 0 then
                                    table.insert(get_card, joker.card)
                                    joker.as_value = 1
                                end
                            end
                        end
                    end
                    if #get_card == 3 then return true, get_card end
                else
                    if #get_card < 5 then
                        if #get_card + #jokers >= 5 then
                            for index, joker in ipairs(jokers) do
                                if #get_card < 5 and joker.as_value == 0 then
                                    table.insert(get_card, joker.card)
                                    joker.as_value = 1
                                end
                            end
                        end
                    end
                    if #get_card == 5 then return true, get_card end
                end
            end
        end
    else
        local one_value_cards = {}
        for index, card in ipairs(cards) do
            local card_value = calculate_cardvalue(card)
            local is_have = check_is_have(one_value_cards, card_value)
            if is_have == false and card < 501 then table.insert(one_value_cards, {card = card, card_value = card_value }) end
        end
        if #one_value_cards < 3 or (#one_value_cards == 4) then
            return false, {}
        end
        for i = 1, #one_value_cards do
            local card_value = calculate_cardvalue(one_value_cards[i].card)
            get_card = {}
            table.insert(get_card, one_value_cards[i].card)
            for j = i + 1, #one_value_cards do
                if card_value == (calculate_cardvalue(one_value_cards[j].card) + 1) then
                    if all_cards_num == 3 then
                        if #get_card < 3 then table.insert(get_card, one_value_cards[j].card) end
                        if #get_card == 3 then return true, get_card end
                    else
                        if #get_card < 5 then table.insert(get_card, one_value_cards[j].card) end
                        if #get_card == 5 then return true, get_card end
                    end
                    card_value = calculate_cardvalue[one_value_cards[j].card]
                else
                    break
                end
            end
        end
    end
    return is_have, get_card
end

----是否是三条
function cardTool.is_santiao(cards, is_has_joker)
    if not cards or not is_has_joker then return false end
    if is_has_joker == 2 then
        local jokers = {}
        for index, card in ipairs(cards) do
            if card >= 501 then table.insert(jokers, card) end
        end
        local num = 0
        local card_value = 0
        for i = 1, #cards do
            num = 1
            if cards[i] < 501 then
                card_value = calculate_cardvalue(cards[i])
                for j = i+1, #cards do
                    if cards[j] < 501 then
                        if card_value == calculate_cardvalue(cards[j]) then
                            num = num + 1
                        end
                    end 
                end
            end
            if num + #jokers == 3 then
                return true
            end
        end
    else
        local num = 0
        local card_value = 0
        for i = 1, #cards do
            num = 1
            card_value = calculate_cardvalue(cards[i])
            for j = i+1, #cards do
                if card_value == calculate_cardvalue(cards[j]) then
                    num = num + 1
                end 
            end
            if num == 3 then
                return true
            end
        end
    end
    return false
end
----获取三条
function cardTool.get_st_incards(cards, is_has_joker)
    local is_have, get_card = false, {}
    if not cards or #cards < 3 then return is_have, get_card end
    local cards_num = #cards
    if cards_num == 3 then
        local temp_cards = tabletool.deepcopy(cards)
        local is_st, st_get_card = cardTool.get_threecard_incards(temp_cards, is_has_joker)
        if is_st == true then
            return true, st_get_card
        end
    else
        local temp_cards = tabletool.deepcopy(cards)
        local is_st, st_get_card = cardTool.get_threecard_incards(temp_cards, is_has_joker)
        if is_st == true then
            local temp_tempcards = tabletool.deepcopy(temp_cards)
            temp_cards = {}
            for _, tempcard in ipairs(temp_tempcards) do
                local is_equal = false
                for _, card in ipairs(st_get_card) do
                    if card == tempcard then
                        is_equal = true
                        break
                    end
                end
                if is_equal == false then table.insert(temp_cards, tempcard) end
            end
            for index, card in ipairs(st_get_card) do
                table.insert(get_card, card)
            end
            ---
            if #temp_cards > 0 and #get_card == 3 then
                for inde, tempcard in ipairs(temp_cards) do
                    if #get_card < 5 then
                        table.insert(get_card, tempcard)
                    end
                end
                if #get_card == 5 then return true, get_card end
            end 
        end
    end
    return is_have, get_card
end

---是否是两对
function cardTool.is_twopair(cards, is_has_joker)
    if not cards or not is_has_joker then return false end
    local pairs_num = 0
    local temp_cards = tabletool.deepcopy(cards)
    for i = 1, 3 do
        local is_have_pairs, get_card = cardTool.get_pairs_incards(temp_cards, is_has_joker)
        if is_have_pairs == true then
            local temp_tempcards = tabletool.deepcopy(temp_cards)
            pairs_num =  pairs_num + 1
            temp_cards = {}
            for _, tempcard in ipairs(temp_tempcards) do
                local is_equal = false
                for _, card in ipairs(get_card) do
                    if card == tempcard then
                        is_equal = true
                        break
                    end
                end
                if is_equal == false then table.insert(temp_cards, tempcard) end
            end
        end
    end
    if pairs_num == 2 then
        return true
    end
    return false
end
---获取两对
function cardTool.get_ld_incards(cards, is_has_joker, is_force)
    local is_have, get_card = false, {}
    if not cards or #cards < 5 then return is_have, get_card end
    local pairs_num = 0
    local temp_cards = tabletool.deepcopy(cards)
    for i = 1, 3 do
        local is_have_pairs, get_pairs_card = cardTool.get_pairs_incards(temp_cards, is_has_joker)
        if is_have_pairs == true then
            local temp_tempcards = tabletool.deepcopy(temp_cards)
            pairs_num = pairs_num + 1
            temp_cards = {}
            for _, tempcard in ipairs(temp_tempcards) do
                local is_equal = false
                for _, card in ipairs(get_pairs_card) do
                    if card == tempcard then
                        is_equal = true
                        break
                    end
                end
                if is_equal == false then table.insert(temp_cards, tempcard) end
            end
            for index, card in ipairs(get_pairs_card) do
                table.insert(get_card, card)
            end
        end
    end
    if pairs_num == 2 then
        if #get_card == 4 then
            if #temp_cards > 0 then
                for index, card in ipairs(temp_cards) do
                    if #get_card < 5 then table.insert(get_card, card) end
                end
                if #get_card == 5 then 
                    is_have = true
                    return is_have, get_card 
                end
            end
        end
    else 
        if is_force == true then
            if pairs_num == 3 and #get_card == 6 then
                table.remove(get_card, 6)
                if #get_card == 5 then
                    is_have = true
                    return is_have, get_card
                end
            end
        end
    end
    return is_have, get_card
end

---是否是一对
function cardTool.is_onepair(cards, is_has_joker)
    if not cards or not is_has_joker then return false end
    if is_has_joker == 2 then
        local jokers = {}
        for index, card in ipairs(cards) do
            if card >= 501 then table.insert(jokers, card) end
        end
        local num = 0
        local card_value = 0
        for i = 1, #cards do
            num = 1
            if cards[i] < 501 then
                card_value = calculate_cardvalue(cards[i])
                for j = i+1, #cards do
                    if cards[j] < 501 then
                        if card_value == calculate_cardvalue(cards[j]) then
                            num = num + 1
                        end
                    end 
                end
            end
            if num + #jokers == 2 then
                return true
            end
        end
    else
        local num = 0
        local card_value = 0
        for i = 1, #cards do
            num = 1
            card_value = calculate_cardvalue(cards[i])
            for j = i+1, #cards do
                if card_value == calculate_cardvalue(cards[j]) then
                    num = num + 1
                end 
            end
            if num == 2 then
                return true
            end
        end
    end
    return false
end
-----获取一对
function cardTool.get_yd_incards(cards, is_has_joker)
    local is_have, get_card = false, {}
    if not cards or #cards < 3 then return is_have, get_card end
    local cards_num = #cards
    local temp_cards = tabletool.deepcopy(cards) 
    local is_has_pair, get_pairs_card = cardTool.get_pairs_incards(temp_cards, is_has_joker)
    if is_has_pair == true then
        local temp_tempcards = tabletool.deepcopy(temp_cards)
        temp_cards = {}
        for _, tempcard in ipairs(temp_tempcards) do
            local is_equal = false
            for _, card in ipairs(get_pairs_card) do
                if card == tempcard then
                    is_equal = true
                    break
                end
            end
            if is_equal == false then table.insert(temp_cards, tempcard) end
        end
        if cards_num == 3 then
            for index, tempcard in ipairs(temp_cards) do
                if #get_card < 3 then
                    table.insert(get_card, tempcard)
                end
            end
            if #get_card == 3 then return true, get_card end
        else
            if cards_num >= 5 then
                for index, tempcard in ipairs(temp_cards) do
                    if #get_card < 5 then table.insert(get_card, tempcard) end
                end
                if #get_card == 5 then return true, get_card end
            end
        end
    end
    return is_have, get_card
end

--判断是否是乌龙
function cardTool.is_wulong(cards, is_has_joker)
    if not cards or not is_has_joker then return false end
    if #cards == 3 then
        if is_has_joker == 2 then
            local jokers = {}
            for index, card in ipairs(cards) do
                if card >= 501 then table.insert(jokers, card) end
            end
            if #jokers > 0 then
                return false
            end
            return true
        else
            local has_pairs = cardTool.has_pairs(cards)
            if has_pairs == true then
                return false
            end
            return true
        end
    elseif #cards == 5 then
        if is_has_joker == 2 then
            local jokers = {}
            for index, card in ipairs(cards) do
                if card >= 501 then table.insert(jokers, card) end
            end
            if #jokers > 0 then
                return false
            end
            return true
        else
            local has_pairs = cardTool.has_pairs(cards)
            if has_pairs == true then
                return false
            end
            return true
        end
    end
end
---获取乌龙
function cardTool.get_wl_incards(cards, is_has_joker)
    local is_have, get_card = false, {}
    if not cards or #cards == 0 then return is_have, get_card end
    if #cards == 3 then
        is_have = true
        for index, card in ipairs(cards) do
            table.insert(get_card, card)
        end
    elseif #cards == 8 or #cards == 13 then
        is_have = true
        for index, card in ipairs(cards) do
            if #get_card < 5 then
                table.insert(get_card, card)
            end
        end
    end
    return is_have, get_card
end

--判断是否是至尊清龙
function cardTool.is_zhizunqinglong(cards, is_has_joker)
    if not cards or #cards ~= SSSCARDNUM then return false end
    local temp_cards = tabletool.deepcopy(cards)
    if is_has_joker == 2 then
        local jokers = {}
        for index, card in ipairs(temp_cards) do
            if card >= 501 then table.insert(jokers, table.remove(temp_cards, index)) end
        end
        local is_tonghua = cardTool.is_withflowers(temp_cards)
        if is_tonghua == false then
            return false
        end
        local has_pairs = cardTool.has_pairs(temp_cards)
        if has_pairs == true then
            return false
        end
        return true
    else
        local is_tonghua = cardTool.is_withflowers(temp_cards)
        if is_tonghua == false then
            return false
        end
        local is_shunzi = cardTool.is_flush(temp_cards)
        if is_shunzi == false then
            return false
        end
        return true
    end
end

---判断是否是一条龙
function cardTool.is_yitiaolong(cards, is_has_joker)
    if not cards or #cards ~= SSSCARDNUM then return false end
    local temp_cards = tabletool.deepcopy(cards)
    if is_has_joker == 2 then
        local jokers = {}
        for index, card in ipairs(temp_cards) do
            if card >= 501 then table.insert(jokers, table.remove(temp_cards, index)) end
        end
        local has_pairs = cardTool.has_pairs(temp_cards)
        if has_pairs == true then
            return false
        end
        return true
    else
        local is_shunzi = cardTool.is_flush(temp_cards)
        if is_shunzi == false then
            return false
        end
        return true
    end
end

----判断是否是十二皇族
function cardTool.is_shierhuangzu(cards, is_has_joker)
    if not cards or #cards ~= SSSCARDNUM then return false end
    local temp_cards = tabletool.deepcopy(cards)
    if is_has_joker == 2 then
        local jokers = {}
        for index, card in ipairs(temp_cards) do
            if card >= 501 then table.insert(jokers, table.remove(temp_cards, index)) end
        end
        local above_ten_num = 0
        for index, card in ipairs(temp_cards) do
            local card_value = calculate_cardvalue(card)
            if card_value >= 11 and card_value <= 13 then
                above_ten_num = above_ten_num + 1
            end
        end
        if above_ten_num + #jokers >= 12 then
            return true
        end
        return false
    else
        local above_ten_num = 0
        for index, card in ipairs(temp_cards) do
            local card_value = calculate_cardvalue(card)
            if card_value >= 11 and card_value <= 13 then
                above_ten_num = above_ten_num + 1
            end
        end
        if above_ten_num >= 12 then
            return true
        end
        return false
    end
end
---判断是否是三同花顺
function cardTool.is_sths(cards, is_has_joker)
    if not cards or #cards ~= SSSCARDNUM then return false end
    ----先判断是否是三顺子
    local temp_cards = tabletool.deepcopy(cards)
    local is_san_tonghua = cardTool.is_santonghua(temp_cards, is_has_joker)
    --filelog.sys_error("------cardTool.is_sths-------is_san_tonghua-----", is_san_tonghua)
    if is_san_tonghua == false then
        return false
    end
    local is_san_shunzi = cardTool.is_sanshunzi(temp_cards, is_has_joker)
    --filelog.sys_error("-----cardTool.is_sths-------", is_san_shunzi)
    if is_san_shunzi == false then
        return false
    end

    return true
end
---判断是否是三分天下
function cardTool.is_sftx(cards, is_has_joker)
    if not cards or #cards ~= SSSCARDNUM then return false end
    local temp_cards = tabletool.deepcopy(cards)
    local bomb_num = 0
    for i = 1, 3 do
        local is_have_boom, get_card = cardTool.get_bomb_incards(temp_cards, is_has_joker)
        local temp_tempcards = tabletool.deepcopy(temp_cards)
        if is_have_boom == true then
            bomb_num = bomb_num + 1
            temp_cards = {}
            for _, tempcard in ipairs(temp_tempcards) do
                local is_equal = false
                for _, card in ipairs(get_card) do
                    if card == tempcard then
                        is_equal = true
                        break
                    end
                end
                if is_equal == false then table.insert(temp_cards, tempcard) end
            end
        end
    end
    if bomb_num == 3 then
        return true
    end
    return false
end
---判断是否是全大(8-A)
function cardTool.is_allbig(cards, is_has_joker)
    if not cards or #cards ~= SSSCARDNUM then return false end
    if is_has_joker == 2 then
        local jokers = {}
        for index, card in ipairs(cards) do
            if card >= 501 then table.insert(jokers, card) end
        end
        local is_allbig = true
        for i = 1, #cards do
            if cards[i] < 501 then
                if calculate_cardvalue(cards[i]) < 8 and calculate_cardvalue(cards[i]) >= 2 then
                    is_allbig = false
                    break
                end
            end
        end
        return is_allbig
    else
        local is_allbig = true
        for i = 1, #cards do
            if calculate_cardvalue(cards[i]) < 8 and calculate_cardvalue(cards[i]) >= 2 then
                is_allbig = false
                break
            end
        end
        return is_allbig
    end
end
---判断是否是全小(2-8)
function cardTool.is_allsmall(cards, is_has_joker)
    if not cards or #cards ~= SSSCARDNUM then return false end
    if is_has_joker == 2 then
        local jokers = {}
        for index, card in ipairs(cards) do
            if card >= 501 then table.insert(jokers, card) end
        end
        local is_allsmall= true
        for i = 1, #cards do
            if cards[i] < 501 then
                if calculate_cardvalue(cards[i]) <= 14 and calculate_cardvalue(cards[i]) > 8 then
                    is_allsmall = false
                    break
                end
            end
        end
        return is_allsmall
    else
        local is_allsmall = true
        for i = 1, #cards do
            if calculate_cardvalue(cards[i]) <= 14 and calculate_cardvalue(cards[i]) > 8 then
                is_allsmall = false
                break
            end
        end
        return is_allsmall
    end
end

local function colors_is_cys(colors_nums)
    local is_cys = false
    if not colors_nums then return is_cys end
    local has_colornums = 0
    local square = 0
    local redheart = 0
    local temp_nums = {}
    for key, value in pairs(colors_nums) do
        if value > 0 then
            has_colornums = has_colornums + 1
            if key == 1 or key == 3 then
                square = square + 1
            elseif key == 2 or key == 4 then
                redheart = redheart + 1
            end
        end
    end
    ---filelog.sys_error("--------colors_is_cys-----has_colornums-------", has_colornums, square, redheart, colors_nums)
    if has_colornums > 2 then 
        return false
    else
        if (square == 2 and redheart == 0) or (square == 0 and redheart == 2) then
            return true
        end
    end
    return false
end

---判断是否是凑一色
function cardTool.is_couys(cards, is_has_joker)
    if not cards or #cards ~= SSSCARDNUM then return false end
    local colors_nums = {
        [1] = 0,
        [2] = 0,
        [3] = 0,
        [4] = 0,
    }
    local is_cyss = true
    if is_has_joker == 2 then
        local jokers = {}
        for index, card in ipairs(cards) do
            if card >= 501 then table.insert(jokers, card) end
        end
        for index, card in ipairs(cards) do
            if card < 501 then
                local card_color = calculate_cardtype(card)
                if colors_nums[card_color] then
                    colors_nums[card_color] = colors_nums[card_color] + 1
                end
                local is_right = colors_is_cys(colors_nums) 
                is_cyss = is_right
            end
        end
        --filelog.sys_error("-------cardTool.is_couys----jokers----", colors_nums)
    else
        for index, card in ipairs(cards) do
            local card_color = calculate_cardtype(card)
            if colors_nums[card_color] then
                colors_nums[card_color] = colors_nums[card_color] + 1
            end
            local is_right = colors_is_cys(colors_nums) 
            is_cyss = is_right
        end
        --filelog.sys_error("--------cardTool.is_couys------------", colors_nums)
    end
    return is_cyss
end
---判断是否是四套三条
function cardTool.is_stst(cards, is_has_joker)
    if not cards or #cards ~= SSSCARDNUM then return false end
    local st_cards_all = {}
    local st_num = 0
    local temp_cards = tabletool.deepcopy(cards)
    for i = 1, 4 do
        local is_have, get_st_card = cardTool.get_threecard_incards(temp_cards, is_has_joker)
        ---filelog.sys_error("-------is_have, get_st_card-----", is_have, get_st_card)
        local temp_tempcards = tabletool.deepcopy(temp_cards)
        if is_have == true then
            st_num = st_num + 1
            temp_cards = {}
            for _, tempcard in ipairs(temp_tempcards) do
                local is_equal = false
                for _, card in ipairs(get_st_card) do
                    if card == tempcard then
                        is_equal = true
                        break
                    end
                end
                if is_equal == false then table.insert(temp_cards, tempcard) end
            end
        end
    end
    if #temp_cards == 1 then
        return true
    end

    return false
end
---判断是否是五对三条
---五对三条(5个对子+三张相同的牌型（三张牌冲头))
function cardTool.is_wdst(cards, is_has_joker)
    if not cards or #cards ~= SSSCARDNUM then return false end
    local pairs_cards_all = {}
    local pairs_num = 0
    local three_num = 0
    local temp_cards = tabletool.deepcopy(cards)
    local is_have, get_three_card = cardTool.get_threecard_incards(temp_cards, is_has_joker)
    if is_have == false then
        return false
    end
    three_num = three_num + 1
    local temp_tempcards = tabletool.deepcopy(temp_cards)
    temp_cards = {}
    for _, tempcard in ipairs(temp_tempcards) do
        local is_equal = false
        for _, card in ipairs(get_three_card) do
            if card == tempcard then
                is_equal = true
                break
            end
        end
        if is_equal == false then table.insert(temp_cards, tempcard) end
    end

    for i = 1, 5 do
        local is_has, get_dui_card = cardTool.get_pairs_incards(temp_cards, is_has_joker)
        if is_has == true then
            pairs_num = pairs_num + 1
            local temp_tempcards = tabletool.deepcopy(temp_cards)
            temp_cards = {}
            for _, tempcard in ipairs(temp_tempcards) do
                local is_equal = false
                for _, card in ipairs(get_dui_card) do
                    if card == tempcard then
                        is_equal = true
                        break
                    end
                end
                if is_equal == false then table.insert(temp_cards, tempcard) end
            end
        end
    end
    ---
    ---filelog.sys_error("---------is_wdst----------", pairs_num, three_num)
    if pairs_num == 5 and three_num == 1 then
        return true
    end
    return false
end
---判断是否是六对半
function cardTool.is_ldb(cards, is_has_joker)
    if not cards or #cards ~= SSSCARDNUM then return false end
    local pairs_num = 0
    local temp_cards = tabletool.deepcopy(cards)

    for i = 1, 6 do
        local is_have, pairs_card = cardTool.get_pairs_incards(temp_cards, is_has_joker)
        --filelog.sys_error("---------is_have, pairs_card------", is_have, pairs_card)
        if is_have == true then
            pairs_num = pairs_num + 1
            local temp_tempcards = tabletool.deepcopy(temp_cards)
            temp_cards = {}
            for _, tempcard in ipairs(temp_tempcards) do
                local is_equal = false
                for _, card in ipairs(pairs_card) do
                    if card == tempcard then
                        is_equal = true
                        break
                    end
                end
                if is_equal == false then table.insert(temp_cards, tempcard) end
            end
        end
    end
    --filelog.sys_error("-------pairs_num--------", pairs_num, #temp_cards)
    if pairs_num == 6 and #temp_cards == 1 then
        return true
    end
    return false
end
---判断是否是三顺子
function cardTool.is_sanshunzi(cards, is_has_joker)
    if not cards or #cards ~= SSSCARDNUM then return false end
    local temp_cards = tabletool.deepcopy(cards)
    local shun_zi_num = 0
    for i = 1, 3 do
        local is_have, shun_zi_card = cardTool.get_sz_incards(temp_cards, is_has_joker)
        ---filelog.sys_error("-------is_have, shun_zi_card-----", is_have, temp_cards, shun_zi_card)
        if is_have == true then
            shun_zi_num = shun_zi_num + 1
            local temp_tempcards = tabletool.deepcopy(temp_cards)
            temp_cards = {}
            local temp_shunzi_card = {}
            for index, card in ipairs(shun_zi_card) do
                table.insert(temp_shunzi_card, { card = card, is_get = 0})
            end
            for _, tempcard in ipairs(temp_tempcards) do
                local is_equal = false
                for _, temp_card in ipairs(temp_shunzi_card) do
                    if tempcard == temp_card.card and temp_card.is_get == 0 then
                        is_equal = true
                        temp_card.is_get = 1
                        break
                    end
                end
                if is_equal == false then table.insert(temp_cards, tempcard) end
            end
            ---filelog.sys_error("--------temp_cards-----", temp_cards)
        end
    end
    ---filelog.sys_error("----------is_sanshunzi-------shun_zi_num ------", shun_zi_num)
    if shun_zi_num == 3 then 
        return true
    end
    return false
end
---判断是否是三同花
function cardTool.is_santonghua(cards, is_has_joker)
    if not cards or #cards ~= SSSCARDNUM then return false end
    local is_flower = false
    local temp_cards = tabletool.deepcopy(cards)
    local tonghua_num = 0
    for i = 1, 3 do
        local is_have, get_th_card = cardTool.get_th_incards(temp_cards, is_has_joker)
        --filelog.sys_error("---------cardTool.is_santonghua------", is_have, get_th_card, temp_cards)
        if is_have == true then
            tonghua_num = tonghua_num + 1
            local temp_tempcards = tabletool.deepcopy(temp_cards)
            temp_cards = {}
            local temp_th_card = {}
            for index, card in ipairs(get_th_card) do
                table.insert(temp_th_card, { card = card, is_get = 0})
            end
            for _, tempcard in ipairs(temp_tempcards) do
                local is_equal = false
                for _, temp_card in ipairs(temp_th_card) do
                    if tempcard == temp_card.card and temp_card.is_get == 0 then
                        is_equal = true
                        temp_card.is_get = 1
                        break
                    end
                end
                if is_equal == false then table.insert(temp_cards, tempcard) end
            end
            --filelog.sys_error("-----------temp_cards......------", temp_cards)
        end
    end
    if tonghua_num == 3 then
        is_flower = true
    end
    --filelog.sys_error("---------cardTool.check_specail_card_type.is_santonghua-----", tonghua_num, is_flower)
    return is_flower
end

function cardTool.analysis_special_card_type(seat, is_has_joker)
    cardTool.from_ztoa_sort(seat.cards)
    ---判断是否是至尊清龙
    local is_zhizun = cardTool.is_zhizunqinglong(seat.cards, is_has_joker)
    if is_zhizun == true then
        seat.special_card_type = SSSCardTypeSpecial.CARD_TYPE_ZHIZUNQINGLONG
        return
    end
    ---判断是否是一条龙
    local is_yitiaolong = cardTool.is_yitiaolong(seat.cards, is_has_joker)
    if is_yitiaolong == true then
        seat.special_card_type = SSSCardTypeSpecial.CARD_TYPE_YITIAOLONG
        return
    end
    ---判断是否是十二皇族
    local is_shierhuangzu = cardTool.is_shierhuangzu(seat.cards, is_has_joker)
    if is_shierhuangzu == true then
        seat.special_card_type = SSSCardTypeSpecial.CARD_TYPE_SHIERHUANGZU
        return
    end
    ---判断是否是三同花顺
    local is_sths = cardTool.is_sths(seat.cards, is_has_joker)
    if is_sths == true then
        seat.special_card_type = SSSCardTypeSpecial.CARD_TYPE_SANTONGHUASHUN
        return 
    end
    ---判断是否是三分天下
    local is_sftx = cardTool.is_sftx(seat.cards, is_has_joker)
    if is_sftx == true then
        seat.special_card_type = SSSCardTypeSpecial.CARD_TYPE_SANFENTIANXIA
        return
    end
    ---判断是否是全大
    local is_allbig = cardTool.is_allbig(seat.cards, is_has_joker)
    if is_allbig == true then
        seat.special_card_type = SSSCardTypeSpecial.CARD_TYPE_QUANDA
    end
    ---判断是否是全小
    local is_allsmall = cardTool.is_allsmall(seat.cards, is_has_joker)
    if is_allsmall == true then
        seat.special_card_type = SSSCardTypeSpecial.CARD_TYPE_QUANXIAO
        return 
    end
    ---判断是否是凑一色
    local is_couys = cardTool.is_couys(seat.cards, is_has_joker)
    if is_couys == true then
        seat.special_card_type = SSSCardTypeSpecial.CARD_TYPE_CHOUYISE
        return
    end
    ---判断是否是四套三条
    local is_stst = cardTool.is_stst(seat.cards, is_has_joker)
    --filelog.sys_error("-------is_stst------------", is_stst)
    if is_stst == true then
        seat.special_card_type = SSSCardTypeSpecial.CARD_TYPE_SITAOSANTIAO
        return
    end
    ---判断是否是五对三条
    local is_wdst = cardTool.is_wdst(seat.cards, is_has_joker)
    if is_wdst == true then
        seat.special_card_type = SSSCardTypeSpecial.CARD_TYPE_WUDUISANTIAO
        return
    end
    ---判断是否是六对半
    local is_ldb = cardTool.is_ldb(seat.cards, is_has_joker)
    if is_ldb == true then
        seat.special_card_type = SSSCardTypeSpecial.CARD_TYPE_LIUDUIBAN
        return
    end
    ---判断是否是三顺子
    local is_sanshunzi = cardTool.is_sanshunzi(seat.cards, is_has_joker)
    if is_sanshunzi == true then
        seat.special_card_type = SSSCardTypeSpecial.CARD_TYPE_SANSHUNZI
        return
    end
    ---判断是否是三同花
    local is_santonghua = cardTool.is_santonghua(seat.cards, is_has_joker)
    if is_santonghua == true then
        seat.special_card_type = SSSCardTypeSpecial.CARD_TYPE_SANTONGHUA
        return
    end
end

---分析正常牌型
function cardTool.analysis_normal_card_type(sorted_cards_item, is_has_joker)
    if not sorted_cards_item or not sorted_cards_item.cards then return end
    if #sorted_cards_item.cards ~= 3 and #sorted_cards_item.cards ~= 5 then return end
    cardTool.from_ztoa_sort(sorted_cards_item.cards)
    if #sorted_cards_item.cards == 3 then
        ---判断是否是三条
        local is_santiao = cardTool.is_santiao(sorted_cards_item.cards, is_has_joker)
        if is_santiao == true then
            sorted_cards_item.card_type = SSSCardType.CARD_TYPE_SANTIAO
            return
        end
        --[[
        ---判断是否是同花
        local is_tonghua = cardTool.is_withflowers(sorted_cards_item.cards, is_has_joker)
        filelog.sys_error("----------is_tonghua----ssssss-----", is_tonghua)
        if is_tonghua == true then
            sorted_cards_item.card_type = SSSCardType.CARD_TYPE_TONGHUA
            return
        end
        --]]
        ---判断是否是一对
        local is_onepair = cardTool.is_onepair(sorted_cards_item.cards, is_has_joker)
        if is_onepair == true then
            sorted_cards_item.card_type = SSSCardType.CARD_TYPE_YIDUI
            return
        end
        ---判断是否是乌龙
        local is_wulong = cardTool.is_wulong(sorted_cards_item.cards, is_has_joker)
        if is_wulong == true then
            sorted_cards_item.card_type = SSSCardType.CARD_TYPE_WULONG
            return
        end
    else
        ---判断是否是五同
        local is_wutong = cardTool.is_wutong(sorted_cards_item.cards, is_has_joker)
        if is_wutong == true then
            sorted_cards_item.card_type = SSSCardType.CARD_TYPE_WUTONG
            return 
        end
        ---判断是否是同花顺
        local is_tonghuashun = cardTool.is_straightflush(sorted_cards_item.cards, is_has_joker)
        --filelog.sys_error("-----------is_tonghuashun---------", is_tonghuashun, sorted_cards_item.cards)
        if is_tonghuashun == true then
            sorted_cards_item.card_type = SSSCardType.CARD_TYPE_TONGHUASHUN
            return
        end
        ---判断是否是铁支
        local is_tiezhi = cardTool.is_tiezhi(sorted_cards_item.cards, is_has_joker)
        if is_tiezhi == true then
            sorted_cards_item.card_type = SSSCardType.CARD_TYPE_TIEZHI
            return
        end
        ---判断是否是葫芦
        local is_hulu = cardTool.is_bottlegourd(sorted_cards_item.cards, is_has_joker)
        if is_hulu == true then
            sorted_cards_item.card_type = SSSCardType.CARD_TYPE_HULU
            return
        end
        ---判断是否是同花
        local is_tonghua = cardTool.is_withflowers(sorted_cards_item.cards, is_has_joker)
        if is_tonghua == true then
            sorted_cards_item.card_type = SSSCardType.CARD_TYPE_TONGHUA
            return
        end
        ---判断是否是顺子
        local is_shunzi = cardTool.is_flush(sorted_cards_item.cards, is_has_joker)
        if is_shunzi == true then
            sorted_cards_item.card_type = SSSCardType.CARD_TYPE_SHUNZI
            return
        end
        ---判断是否是三条
        local is_santiao = cardTool.is_santiao(sorted_cards_item.cards, is_has_joker)
        if is_santiao == true then
            sorted_cards_item.card_type = SSSCardType.CARD_TYPE_SANTIAO
            return
        end
        ---判断是否是两对
        local is_twopair = cardTool.is_twopair(sorted_cards_item.cards, is_has_joker)
        if is_twopair == true then
            sorted_cards_item.card_type = SSSCardType.CARD_TYPE_LIANGDUI
            return
        end
        ---判断是否是一对
        local is_onepair = cardTool.is_onepair(sorted_cards_item.cards, is_has_joker)
        if is_onepair == true then
            sorted_cards_item.card_type = SSSCardType.CARD_TYPE_YIDUI
            return
        end
        ---判断是否是乌龙
        local is_wulong = cardTool.is_wulong(sorted_cards_item.cards, is_has_joker)
        if is_wulong == true then
            sorted_cards_item.card_type = SSSCardType.CARD_TYPE_WULONG
            return
        end
    end
end

---自动理牌
function cardTool.sort_hand_cards(cards, is_has_joker, special_card_type, deal_as_special)
    local sorted_items_temp = {}
    --filelog.sys_error("-------cardTool.sort_hand_cards------", cards, #cards, is_has_joker)
    if not cards or #cards ~= SSSCARDNUM then
        return sorted_items_temp
    end
    ---init
    for i = 1, 3 do
        table.insert(sorted_items_temp, 
        {
            card_slot_index = i,
            card_type = 0,
            deal_score = 0,
            cards = {},
        })
    end
    cardTool.from_ztoa_sort(cards)
    local temp_cards = tabletool.deepcopy(cards)
    local stand_temp_cards = {}
    if special_card_type == SSSCardTypeSpecial.CARD_TYPE_LIUDUIBAN or special_card_type == SSSCardTypeSpecial.CARD_TYPE_WUDUISANTIAO then
        if special_card_type == SSSCardTypeSpecial.CARD_TYPE_WUDUISANTIAO and #sorted_items_temp[1].cards == 0 then
            local is_sant, sant_cards = cardTool.get_threecard_incards(temp_cards, is_has_joker)
            if is_sant == true then
                stand_temp_cards = tabletool.deepcopy(temp_cards)
                local get_cards_temp = {}
                temp_cards = {}
                for index, card in ipairs(sant_cards) do
                    table.insert(sorted_items_temp[1].cards, card)
                    table.insert(get_cards_temp, { card = card, is_get = 0})
                end
                sorted_items_temp[1].card_type = SSSCardType.CARD_TYPE_SANTIAO
                for _, tempcard in ipairs(stand_temp_cards) do
                    local is_equal = false
                    for _, temp_card in ipairs(get_cards_temp) do
                        if temp_card.card == tempcard and temp_card.is_get == 0 then
                            is_equal = true
                            temp_card.is_get = 1
                            break
                        end
                    end
                    if is_equal == false then table.insert(temp_cards, tempcard) end
                end
            end
            for i = #sorted_items_temp,1,-1 do
                if #sorted_items_temp[i].cards == 0 then
                    local get_item = cardTool.get_big_cardtypes(temp_cards, is_has_joker, special_card_type, deal_as_special)
                    ---filelog.sys_error("------sorted_items_temp--11111----", i, get_item, temp_cards)
                    stand_temp_cards = tabletool.deepcopy(temp_cards)
                    if get_item.card_type ~= 0 then
                        local get_cards_temp = {}
                        temp_cards = {}
                        for index, card in ipairs(get_item.cards) do
                            table.insert(sorted_items_temp[i].cards, card)
                            table.insert(get_cards_temp, {card = card, is_get = 0})
                        end
                        sorted_items_temp[i].card_type = get_item.card_type
                        for _, tempcard in ipairs(stand_temp_cards) do
                            local is_equal = false
                            for _, temp_card in ipairs(get_cards_temp) do
                                if temp_card.card == tempcard and temp_card.is_get == 0 then
                                    is_equal = true
                                    temp_card.is_get = 1
                                    break
                                end
                            end
                            if is_equal == false then table.insert(temp_cards, tempcard) end
                        end
                    end
                    --filelog.sys_error("------sorted_items_temp-after---111111--", i, get_item, temp_cards)
                end
            end
        else
            for i = #sorted_items_temp,1,-1 do
                local get_item = cardTool.get_big_cardtypes(temp_cards, is_has_joker, special_card_type, deal_as_special)
                --filelog.sys_error("------sorted_items_temp--11111----", i, get_item, temp_cards)
                stand_temp_cards = tabletool.deepcopy(temp_cards)
                if get_item.card_type ~= 0 then
                    local get_cards_temp = {}
                    temp_cards = {}
                    for index, card in ipairs(get_item.cards) do
                        table.insert(sorted_items_temp[i].cards, card)
                        table.insert(get_cards_temp, {card = card, is_get = 0})
                    end
                    sorted_items_temp[i].card_type = get_item.card_type
                    for _, tempcard in ipairs(stand_temp_cards) do
                        local is_equal = false
                        for _, temp_card in ipairs(get_cards_temp) do
                            if temp_card.card == tempcard and temp_card.is_get == 0 then
                                is_equal = true
                                temp_card.is_get = 1
                                break
                            end
                        end
                        if is_equal == false then table.insert(temp_cards, tempcard) end
                    end
                end
                --filelog.sys_error("------sorted_items_temp-after---111111--", i, get_item, temp_cards)
            end
        end
    else
        for i = #sorted_items_temp,1,-1 do
            local get_item = cardTool.get_big_cardtypes(temp_cards, is_has_joker, special_card_type, deal_as_special)
            --filelog.sys_error("------sorted_items_temp------", i, get_item, temp_cards)
            stand_temp_cards = tabletool.deepcopy(temp_cards)
            if get_item.card_type ~= 0 then
                local get_cards_temp = {}
                temp_cards = {}
                for index, card in ipairs(get_item.cards) do
                    table.insert(sorted_items_temp[i].cards, card)
                    table.insert(get_cards_temp, {card = card, is_get = 0})
                end
                sorted_items_temp[i].card_type = get_item.card_type
                for _, tempcard in ipairs(stand_temp_cards) do
                    local is_equal = false
                    for _, temp_card in ipairs(get_cards_temp) do
                        if temp_card.card == tempcard and temp_card.is_get == 0 then
                            is_equal = true
                            temp_card.is_get = 1
                            break
                        end
                    end
                    if is_equal == false then table.insert(temp_cards, tempcard) end
                end
            end
            --filelog.sys_error("------sorted_items_temp-after-----", i, get_item, temp_cards)
        end
    end
    for index, item_temp in ipairs(sorted_items_temp) do
        cardTool.analysis_normal_card_type(item_temp, is_has_joker)
    end
    --filelog.sys_error("--------sorted_items_temp------", sorted_items_temp)
    return sorted_items_temp
end

function cardTool.get_big_cardtypes(cards, is_has_joker, special_card_type)
    local get_item = {
        card_type = 0,
        cards = {},
    }
    if not cards then return get_item end
    if special_card_type == 1 then
        ---看是否有同花
        local is_th, th_get_card = cardTool.get_th_incards(cards, is_has_joker)
        ---filelog.sys_error("------special_card_type---is_th, th_get_card----------", is_th, special_card_type, th_get_card)
        if is_th == true then
            get_item.cards = {}
            get_item.card_type = SSSCardType.CARD_TYPE_TONGHUA
            for index, card in ipairs(th_get_card) do
                table.insert(get_item.cards, card)
            end
            return get_item
        end
    elseif special_card_type == 2 then
        ---看是否有顺子
        local is_shunzi, sz_get_card = cardTool.get_sz_incards(cards, is_has_joker)
        --filelog.sys_error("---------is_shunzi, sz_get_card-------", is_shunzi, sz_get_card)
        if is_shunzi == true then
            get_item.cards = {}
            get_item.card_type = SSSCardType.CARD_TYPE_SHUNZI
            for index, card in ipairs(sz_get_card) do
                table.insert(get_item.cards, card)
            end
            return get_item
        end
    elseif special_card_type == 4 or special_card_type == 3 then
        local is_ld, ld_get_card = cardTool.get_ld_incards(cards, is_has_joker, true)
        --filelog.sys_error("----------is_ld, ld_get_card--------111111----", is_ld, ld_get_card)
        if is_ld == true then
            get_item.cards = {}
            get_item.card_type = SSSCardType.CARD_TYPE_LIANGDUI
            for index, card in ipairs(ld_get_card) do
                table.insert(get_item.cards, card)
            end
            return get_item
        end
    end
    ---先看有没有五同
    local is_wutong, get_card = cardTool.get_wt_incards(cards, is_has_joker)
    if is_wutong == true then
        get_item.cards = {}
        get_item.card_type = SSSCardType.CARD_TYPE_WUTONG
        for index, card in ipairs(get_card) do
            table.insert(get_item.cards, card)
        end
        return get_item
    end
    ---看是否是同花顺
    local is_ths, t_get_card = cardTool.get_ths_incards(cards, is_has_joker)
    ---filelog.sys_error("------is_ths, t_get_card-----", is_ths, t_get_card)
    if is_ths == true then
        get_item.cards = {}
        get_item.card_type = SSSCardType.CARD_TYPE_TONGHUASHUN
        for index, card in ipairs(t_get_card) do
            table.insert(get_item.cards, card)
        end
        return get_item
    end
    --- 看是否有铁支
    local is_tz, tz_get_card = cardTool.get_tz_incards(cards, is_has_joker)
    if is_tz == true then
        get_item.cards = {}
        get_item.card_type = SSSCardType.CARD_TYPE_TIEZHI
        for index, card in ipairs(tz_get_card) do
            table.insert(get_item.cards, card)
        end
        return get_item
    end
    ---看是否有葫芦
    local is_hl, hl_get_card = cardTool.get_hl_incards(cards, is_has_joker)
    if is_hl == true then
        get_item.cards = {}
        get_item.card_type = SSSCardType.CARD_TYPE_HULU
        for index, card in ipairs(hl_get_card) do
            table.insert(get_item.cards, card)
        end
        return get_item
    end
    ---看是否有同花
    local is_th, th_get_card = cardTool.get_th_incards(cards, is_has_joker)
    ---filelog.sys_error("---------is_th, th_get_card----------", is_th, th_get_card)
    if is_th == true then
        get_item.cards = {}
        get_item.card_type = SSSCardType.CARD_TYPE_TONGHUA
        for index, card in ipairs(th_get_card) do
            table.insert(get_item.cards, card)
        end
        return get_item
    end 
    ---看是否有顺子
    local is_sz, sz_get_card = cardTool.get_sz_incards(cards, is_has_joker)
    if is_sz == true then
        get_item.cards = {}
        get_item.card_type = SSSCardType.CARD_TYPE_SHUNZI
        for index, card in ipairs(sz_get_card) do
            table.insert(get_item.cards, card)
        end
        return get_item
    end
    ---看是否有三条
    local is_st, st_get_card = cardTool.get_st_incards(cards, is_has_joker)
    if is_st == true then
        get_item.cards = {}
        get_item.card_type = SSSCardType.CARD_TYPE_SANTIAO
        for index, card in ipairs(st_get_card) do
            table.insert(get_item.cards, card)
        end
        return get_item
    end
    ---看是否有两对
    local is_ld, ld_get_card = cardTool.get_ld_incards(cards, is_has_joker)
    if is_ld == true then
        get_item.cards = {}
        get_item.card_type = SSSCardType.CARD_TYPE_LIANGDUI
        for index, card in ipairs(ld_get_card) do
            table.insert(get_item.cards, card)
        end
        return get_item
    end
    ---看是否有一对
    local is_yd, yd_get_card = cardTool.get_yd_incards(cards, is_has_joker)
    ---filelog.sys_error("-------is_yd, yd_get_card--------", is_yd, yd_get_card)
    if is_yd == true then
        get_item.cards = {}
        get_item.card_type = SSSCardType.CARD_TYPE_YIDUI
        for index, card in ipairs(yd_get_card) do
            table.insert(get_item.cards, card)
        end
        return get_item
    end
    ---看是否有乌龙
    local is_wl, wl_get_card = cardTool.get_wl_incards(cards, is_has_joker)
    if is_wl == true then
        get_item.cards = {}
        get_item.card_type = SSSCardType.CARD_TYPE_WULONG
        for index, card in ipairs(wl_get_card) do
            table.insert(get_item.cards, card)
        end
        return get_item
    end
    return get_item
end

function cardTool.compare_two_sorted_items(sorted_item_one, sorted_item_two, is_has_joker)
    local is_bigger = 0 
    if not sorted_item_one or not sorted_item_two then return is_bigger end
    if sorted_item_one.card_type == 0 or sorted_item_two.card_type == 0 then return is_bigger end
    if sorted_item_one.card_type > sorted_item_two.card_type then
        is_bigger = 1
    elseif sorted_item_one.card_type < sorted_item_two.card_type then
        is_bigger = 2
    elseif sorted_item_one.card_type == sorted_item_two.card_type then
        ---牌型相同,比较牌值
        local one_cards = sorted_item_one.cards
        local two_cards = sorted_item_two.cards
        if is_has_joker == 2 then
            local joker_cards_one = {}
            local joker_cards_two = {}
            for index, card in ipairs(one_cards) do
                if card >= 501 then table.insert(joker_cards_one, card) end
            end
            for index, card in ipairs(two_cards) do
                if card >= 501 then table.insert(joker_cards_two, card) end
            end
            cardTool.from_ztoa_sort(one_cards)
            cardTool.from_ztoa_sort(two_cards)
            for i = 1, 5 do
                if one_cards[i] and two_cards[i] and one_cards[i] then
                    if calculate_cardvalue(one_cards[i]) > calculate_cardvalue(two_cards[i]) then
                        is_bigger = 1
                        break
                    elseif calculate_cardvalue(one_cards[i]) < calculate_cardvalue(two_cards[i]) then
                        is_bigger = 2
                        break
                    end
                end
            end
            if #joker_cards_one == #joker_cards_two then
                is_bigger = 0
            elseif #joker_cards_one > #joker_cards_two then
                is_bigger = 2
            elseif #joker_cards_one < #joker_cards_two then
                is_bigger = 1
            end
        else
            cardTool.from_ztoa_sort(one_cards)
            cardTool.from_ztoa_sort(two_cards)
            if #one_cards == #two_cards then
                for i = 1, #one_cards do
                    if calculate_cardvalue(one_cards[i]) > calculate_cardvalue(two_cards[i]) then
                        is_bigger = 1
                        break
                    elseif calculate_cardvalue(one_cards[i]) < calculate_cardvalue(two_cards[i]) then
                        is_bigger = 2
                        break
                    end
                end
            end
        end
    end
    return is_bigger
end

function cardTool.compare_special_type_cards(cards1, cards2, is_has_joker)
    if not cards1 or #cards1 ~= SSSCARDNUM then return 0 end
    if not cards2 or #cards2 ~= SSSCARDNUM then return 0 end
    cardTool.from_ztoa_sort(cards1)
    cardTool.from_ztoa_sort(cards2)
    local jokers_one = {}
    local jokers_two = {}
    for index, card in ipairs(cards1) do
        if card >= 501 then table.insert(jokers_one, card) end
    end
    for index, card in ipairs(cards2) do
        if card >= 501 then table.insert(jokers_two, card) end
    end
    --cardTool.from_ztoa_sort(cards1)
    --cardTool.from_ztoa_sort(cards2)
    for i = 1, #cards1 do
        if cards1[i] and cards2[i] and cards1[i] < 501 and cards2[i] < 501 then
            if calculate_cardvalue(cards1[i]) > calculate_cardvalue(cards2[i]) then
                return 1
            elseif calculate_cardvalue(cards1[i]) < calculate_cardvalue(cards2[i]) then
                return 2
            end
        end
    end
    if #jokers_one == #jokers_two then
        return 0
    elseif #jokers_one > #jokers_two then
        return 2
    elseif #jokers_one < #jokers_two then
        return 1
    end
end

return cardTool